package com.rubiksillusions;

import java.util.*;

public class FaceletsString {
	private String faceletsString;
	private ArrayList<String> masks = new ArrayList<String>();
	private ArrayList<String> generatedStrings = new ArrayList<String>();
	
	public static final int COMBINATORY = 0;
	
	public FaceletsString(String faceletsString) {
		this.faceletsString = faceletsString;
	}
	
	public void addMask(String mask) {
		masks.add(mask);
		calculateGeneratedStrings();
	}
	
	private void calculateGeneratedStrings() {
		generatedStrings = new ArrayList<String>();
		generatedStrings.add(faceletsString);
		
		for (String mask : masks) {
			String permutationString = "";
			ArrayList<String> oldGeneratedStrings = generatedStrings;
			int[] permutationStringIndex = new int[54];
			generatedStrings = new ArrayList<String>();
			int j = 0;
			for (int i = 0; i < mask.length(); i++) {
				if (mask.charAt(i) != '.') {
					permutationString += faceletsString.charAt(i);
					permutationStringIndex[i] = j++;
				} else {
					permutationStringIndex[i] = 0;
				}
			}
			String[] permutations = getPermutationsArray(permutationString);
			for (String generatedString : oldGeneratedStrings) {
				for (String permutation : permutations) {
					String newGeneratedString = "";
					for (int i = 0; i < generatedString.length(); i++) {
						if (permutationStringIndex[i] == 0) {
							newGeneratedString += generatedString.charAt(i);
						} else {
							newGeneratedString += permutation.charAt(permutationStringIndex[i]);
						}
					}
					generatedStrings.add(newGeneratedString);
				}
			}
		}
	}
	
	/*
	 * Genero una stringa per ogni combinazione di mask COMBINATION.
	 */
	public ArrayList<String> getGeneratedStringsArrayList() {
		return generatedStrings;
	}
	
	public String[] getGeneratedStringsArray() {
		return generatedStrings.toArray(new String[] {});
	}
	
	private String[] getPermutationsArray(String s) {
		ArrayList<String> permutationsArrayList = new ArrayList<String>();
		perm1(s, permutationsArrayList);
		return permutationsArrayList.toArray(new String[] {});
	}
	
	private void perm1(String s, ArrayList<String> permutationsArrayList) {
		perm1("", s, permutationsArrayList);
	}
	
	private void perm1(String prefix, String s, ArrayList<String> permutationsArrayList) {
		int N = s.length();
		if (N == 0) {
			permutationsArrayList.add(new String(prefix));
		} else {
			for (int i = 0; i < N; i++) {
				perm1(prefix + s.charAt(i), s.substring(0, i) + s.substring(i+1, N), permutationsArrayList);
			}
        }
    }
	
	public static void main(String[] args) {
		FaceletsString fs = new FaceletsString("wwwwwwwwwyyyyyyyyybbbbbbbbbooooooooogggggggggrrrrrrrrr");
		fs.addMask("....1.......1.......1.................................");
		fs.addMask("......................................2............2..");
		fs.addMask(".......................................2.............3");
		for (String s : fs.getGeneratedStringsArray()) {
			System.out.println(s);
		}
	}
}
